﻿using sydfdl.TMS.BLL;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Mvc;

namespace sydfdl.TMS.Areas.SystemData.Controllers
{
    public class UserController : Controller
    {

        BLLT_B_User bllUser = new BLLT_B_User();

        #region  页面

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 用户权限选择页面
        /// </summary>
        /// <returns></returns>
        public ActionResult User_Menu()
        {
            return View();
        }


        #endregion

        #region 方法

        /// <summary>
        /// 获取所有用户
        /// </summary>
        public void GetAllUsers()
        {
            string xml = JsonHelp.GetParaXML(Request.Form.Get("pageIndex"), Request.Form.Get("pageSize"),
                Request.Form.Get("sortField"), Request.Form.Get("sortOrder"), Request.Form.Get("key"));

            var list = bllUser.GetAllUserList(xml);

            string str = JsonHelp.EntitiesToString(list);

            Response.Write(str);
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public void SaveData()
        {
            string json = @"[]";

            IEnumerable<T_B_User> add = Request.Form.Get("add") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_User>>(Request.Form.Get("add"));
            IEnumerable<T_B_User> edit = Request.Form.Get("edit") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_User>>(Request.Form.Get("edit"));
            IEnumerable<T_B_User> remove = Request.Form.Get("remove") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_User>>(Request.Form.Get("remove"));

            string str = bllUser.SaveUserData(add, edit, remove);

            Response.Write(str);
        }

        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <param name="uid"></param>
        public void GetAllMenuByUser(string uid)
        {
            var dt = bllUser.GetUserMenu(uid);

            string str = MiniUIJson.Encode(dt);

            Response.Write(str);
        }

        public void SaveUserMenu()
        {
            string jsonDecode = HttpUtility.UrlDecode(Request.Form.Get("edit"));
            string uid = Request.Form.Get("uid");

            string json = @"[]";

            IEnumerable<T_B_UserMenu> edit = jsonDecode == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_UserMenu>>(jsonDecode);

            string str = bllUser.SaveUserMenuData(edit, uid);

            Response.Write(str);
        }

        #endregion


    }
}
